package cn.com.xf.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.dao.BatDstareaDAO;
import cn.com.xf.dao.TbAppAreasDAO;
import cn.com.xf.dao.TbAppAuditingAreasDAO;
import cn.com.xf.dao.TbareainfoDAO;
import cn.com.xf.dao.TblOaAreaCoordinateDAO;
import cn.com.xf.dao.TblOaAuthRoleDAO;
import cn.com.xf.dao.VDstareaDAO;
import cn.com.xf.domain.BatDstarea;
import cn.com.xf.domain.TbAppAreas;
import cn.com.xf.domain.TbAppAuditingAreas;
import cn.com.xf.domain.Tbareainfo;
import cn.com.xf.domain.TblOaAreaCoordinate;
import cn.com.xf.domain.TblOaAuthRole;
import cn.com.xf.domain.VDstarea;
import cn.com.xf.service.AreaService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.form.AreaForm;
import cn.com.xf.web.form.AreaMapForm;

@Service
public class AreaServiceImpl implements AreaService {
	@Autowired
	private BatDstareaDAO batDstareaDAO;
	@Autowired
	private VDstareaDAO vDstareaDAO;
	@Autowired
	private TbareainfoDAO tbareainfoDAO;
	@Autowired
	private TblOaAreaCoordinateDAO tblOaAreaCoordinateDAO;
	@Autowired
	private TbAppAreasDAO appAreasDAO;
	@Autowired
	private TbAppAuditingAreasDAO appAuditingAreasDAO;
	@Autowired
	private TblOaAuthRoleDAO oaAuthRoleDAO;
	@Override
	public List<BatDstarea> selectBatDstarea(int begin, int pageSize, String orderByField, String orderByMode,
			String siteId, String siteName, String markId, String markName, String areaId, String areaName) {
		return batDstareaDAO.selectBatDstarea(begin, pageSize, orderByField, orderByMode, siteId, siteName, markId,
				markName, areaId, areaName);
	}

	@Override
	public int selectBatDstareaCount(String siteId, String siteName, String markId, String markName, String areaId,
			String areaName) {
		return batDstareaDAO.selectBatDstareaCount(siteId, siteName, markId, markName, areaId, areaName);
	}

	@Override
	public BatDstarea selectByFldidd(String fldidd) {
		return batDstareaDAO.selectByPrimaryKey(new Integer(fldidd));
	}

	@Override
	public VDstarea selectVDstareaByFldidd(String fldidd) {
		return vDstareaDAO.selectByFldidd(fldidd);
	}

	@Override
	public List<Tbareainfo> selectTbareainfoListByAreaName(String areaName) {
		return tbareainfoDAO.selectListByAreaName(areaName);
	}
	
	@Override
	public String selectAreaIdByAreaName(String areaName) {
		return tbareainfoDAO.selectByAreaName2(areaName);
	}

	@Override
	public int updateBatDstarea(AreaForm form) {
		// 1.获取fldidd
		String fldiddStr = form.getFldidd();
		String fldidd = EncrypTool.dencode(fldiddStr);
		BatDstarea area = batDstareaDAO.selectByPrimaryKey(new Integer(fldidd));
		if (null == area) {
			return -1;
		}

		// 2.更新编辑的数据
		area.setFldsitenames(form.getFldsitenames().trim());
		area.setFldpys(form.getFldpys().trim());
		area.setFldruntypes(form.getFldruntypes().trim());
		area.setFldofareas(form.getAreaId());
		area.setFldofsites(form.getSiteId());

		String daofu = StringUtils.isNotBlank(form.getFlddaofuflagd()) ? form.getFlddaofuflagd() : "0";
		String daiqian = StringUtils.isNotBlank(form.getFlddaiqianflagd()) ? form.getFlddaiqianflagd() : "0";
		String daishou = StringUtils.isNotBlank(form.getFlddaishouflagd()) ? form.getFlddaishouflagd() : "0";
		String jiri = StringUtils.isNotBlank(form.getFldjiriflagd()) ? form.getFldjiriflagd() : "0";
		String hide = StringUtils.isNotBlank(form.getFldhided()) ? form.getFldhided() : "0";

		area.setFlddaofuflagd(new Short(daofu));
		area.setFlddaiqianflagd(new Short(daiqian));
		area.setFlddaishouflagd(new Short(daishou));
		area.setFldjiriflagd(new Short(jiri));
		area.setFldowners(form.getFldowners());

		area.setFldmobiles(form.getFldmobiles());
		area.setFldfaxs(form.getFldfaxs());
		area.setFldaddresss(form.getFldaddresss());
		area.setFldsortd(new BigDecimal(form.getFldsortd()));
		area.setFldhided(new BigDecimal(hide));

		area.setFldtels(form.getFldtels());
		area.setFldregions(form.getFldregions());
		area.setFldadddayregions(form.getFldadddayregions());
		area.setFldoutregions(form.getFldoutregions());
		area.setFldpubdatetStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		batDstareaDAO.updateByPrimaryKeyWithBLOBs(area);
		return 0;
	}

	@Override
	public int insertBatDstarea(AreaForm form) {

		BatDstarea area = new BatDstarea();

		area.setFldsitenames(form.getFldsitenames().trim());
		area.setFldpys(form.getFldpys().trim());
		area.setFldruntypes(form.getFldruntypes().trim());
		area.setFldofareas(form.getAreaId());
		area.setFldofsites(form.getSiteId());

		String daofu = StringUtils.isNotBlank(form.getFlddaofuflagd()) ? form.getFlddaofuflagd() : "0";
		String daiqian = StringUtils.isNotBlank(form.getFlddaiqianflagd()) ? form.getFlddaiqianflagd() : "0";
		String daishou = StringUtils.isNotBlank(form.getFlddaishouflagd()) ? form.getFlddaishouflagd() : "0";
		String jiri = StringUtils.isNotBlank(form.getFldjiriflagd()) ? form.getFldjiriflagd() : "0";
		String hide = StringUtils.isNotBlank(form.getFldhided()) ? form.getFldhided() : "0";

		area.setFlddaofuflagd(new Short(daofu));
		area.setFlddaiqianflagd(new Short(daiqian));
		area.setFlddaishouflagd(new Short(daishou));
		area.setFldjiriflagd(new Short(jiri));
		area.setFldowners(form.getFldowners());

		area.setFldmobiles(form.getFldmobiles());
		area.setFldfaxs(form.getFldfaxs());
		area.setFldaddresss(form.getFldaddresss());
		area.setFldsortd(new BigDecimal(form.getFldsortd()));
		area.setFldhided(new BigDecimal(hide));

		area.setFldtels(form.getFldtels());
		area.setFldregions(form.getFldregions());
		area.setFldadddayregions(form.getFldadddayregions());
		area.setFldoutregions(form.getFldoutregions());
		area.setFldpubdatetStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		batDstareaDAO.insertSelective(area);

		return 0;
	}

	@Override
	public int selectBatDstareaCountByOfAreaId(String ofAreaId) {
		return batDstareaDAO.selectCountByOfAreaId(ofAreaId);
	}

	@Override
	public TblOaAreaCoordinate selectTblOaAreaCoordinateByAreaId(String areaId) {
		return tblOaAreaCoordinateDAO.selectByPrimaryKey(areaId);
	}

	@Override
	public int insertOrUpdateAreaCoordinate(String areaId, String x, String y) {
		// 检查是否存在
		TblOaAreaCoordinate coor = tblOaAreaCoordinateDAO.selectByPrimaryKey(areaId);
		if (null != coor) {
			if (StringUtils.isNotBlank(coor.getAreaid()) && StringUtils.isNotBlank(coor.getLatitude())
					&& StringUtils.isNotBlank(coor.getLongitude())) {
				coor.setLatitude(y);
				coor.setLongitude(x);
				tblOaAreaCoordinateDAO.updateByPrimaryKeySelective(coor);
				return 0;
			}
		}
		TblOaAreaCoordinate newC = new TblOaAreaCoordinate();
		newC.setAreaid(areaId);
		newC.setLatitude(y);
		newC.setLongitude(x);
		tblOaAreaCoordinateDAO.insert(newC);
		return 0;
	}

	@Override
	public Tbareainfo selectAreainfoByAreaId(String areaID) {
		return tbareainfoDAO.selectAreaAndSiteById(areaID);
	}

	@Override
	public void deleteArea(String areaId, String fldidd) {
		// 1.更新基础表
		Tbareainfo tbareainfo = tbareainfoDAO.selectByPrimaryKey(areaId);
		if (null != tbareainfo) {
			tbareainfo.setRunstatus("2");
			tbareainfoDAO.updateByPrimaryKeySelective(tbareainfo);
		}

		// 2.删除派送区域表
		batDstareaDAO.deleteByPrimaryKey(Integer.valueOf(fldidd.trim()));
	}

	@Override
	public TbAppAreas selectByMapid(String areaid) {
		// TODO Auto-generated method stub
		return appAreasDAO.selectByAreaid(areaid);
	}
	
	@Override
	public TbAppAuditingAreas selectByAuditingMapid(String areaid) {
		// TODO Auto-generated method stub
		return appAuditingAreasDAO.selectByAreaid(areaid);
	}

	@Override
	public int insertMap(AreaMapForm form) {
		String areaname = form.getAreaname();
		String siteid = form.getSiteid();
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		map.put("siteid", siteid);
		List<BatDstarea> list = batDstareaDAO.selectByAreaname(map);
		if(list == null || list.isEmpty()){
			return 0;
		}
		TbAppAreas areasMap = new TbAppAreas();
		areasMap.setAreaid(UUID.randomUUID().toString());
		areasMap.setAreaname(areaname);
		areasMap.setSiteid(siteid);
		areasMap.setProvince(form.getProvince());
		areasMap.setCity(form.getCity());
		areasMap.setCounty("");
		areasMap.setFlag(0);
		areasMap.setIsdeleted(0);
		areasMap.setIschecked(0);
		areasMap.setEnteruser(form.getEnteruser());
		areasMap.setEnterdateStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		areasMap.setRemark(form.getRemark());
		areasMap.setPoints("[{\"lng\":\"113.753442\",\"lat\":\"23.021347\"}]");
		appAreasDAO.insert(areasMap);
		return 1;
	}

	@Override
	public int auditingMap(AreaMapForm form){
		String areaname = form.getAreaname();
		String siteid = form.getSiteid();
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		map.put("siteid", siteid);
		TbAppAuditingAreas areasMap = new TbAppAuditingAreas();
		areasMap.setAreaid(form.getAreaid());
		areasMap.setAreaname(form.getAreaname());
		areasMap.setSiteid(siteid);
		areasMap.setProvince(form.getProvince());
		areasMap.setCity(form.getCity());
		areasMap.setCounty("");
		areasMap.setIsdeleted(0);
		areasMap.setIschecked(0);
		areasMap.setEnteruser(form.getEnteruser());
		areasMap.setEnterdateStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		areasMap.setRemark(form.getRemark());
		areasMap.setPoints(form.getPoints());
		appAuditingAreasDAO.insert(areasMap);
		return 1;
	}
	
	@Override
	public int updateMap(AreaMapForm form) {
		String areaname = form.getAreaname();
		String siteid = form.getSiteid();
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		map.put("siteid", siteid);		
		TbAppAreas areasMap = new TbAppAreas();
		areasMap.setAreaid(form.getAreaid());
		areasMap.setCheckuser(form.getCheckuser());
		//areasMap.setCheckdate(new Date());
		//areasMap.setCheckdateStr(form.getCheckdate());
		areasMap.setCheckdateStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		areasMap.setRemark(form.getRemark());
		areasMap.setPoints(form.getPoints());		
		return appAreasDAO.update(areasMap);
	}

	@Override
	public int updateAuditing(AreaMapForm form) {
		String areaname = form.getAreaname();
		String siteid = form.getSiteid();
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		map.put("siteid", siteid);		
		TbAppAuditingAreas areasMap = new TbAppAuditingAreas();
		areasMap.setAreaid(form.getAreaid());
		areasMap.setCheckuser(form.getCheckuser());
		areasMap.setIsdeleted(0);
		areasMap.setEnterdate(new Date());
		areasMap.setCheckdate(null);
		//areasMap.setCheckdateStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		areasMap.setPoints(form.getPoints());
		return appAuditingAreasDAO.updateAuditing(areasMap);
	}
	
	@Override
	public int updateCheckDate(AreaMapForm form) {
		String areaname = form.getAreaname();
		String siteid = form.getSiteid();
		Map<String,String> map = new HashMap<String, String>();
		map.put("areaname", areaname);
		map.put("siteid", siteid);	
		TbAppAuditingAreas areasMap = new TbAppAuditingAreas();
		areasMap.setAreaid(form.getAreaid());
		areasMap.setCheckdate(new Date());
		//areasMap.setCheckdateStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		return appAuditingAreasDAO.updateCheckDate(areasMap);
	}
	@Override
	public int delMap(String areaid) {
		return appAreasDAO.delByAreaid(areaid);
	}
	@Override
	public int checkMap_1(String areaid){
		return appAuditingAreasDAO.checkByAreaid_1(areaid);
	}
	@Override
	public int checkMap_0(String areaid){
		return appAuditingAreasDAO.checkByAreaid_0(areaid);
	}
	@Override
	public List<TbAppAreas> selectMap(int begin, int pageSize, String orderByField, String orderByMode, String siteid,
			String areaname,String province,String city) {
		// TODO Auto-generated method stub
		return appAreasDAO.selectArea(begin, pageSize, orderByField, orderByMode, siteid, areaname,province,city);
	}
	
	@Override
	public List<TbAppAuditingAreas> selectMapAuditing(int begin, int pageSize, String orderByField, String orderByMode, String siteid,
			String areaname) {
		// TODO Auto-generated method stub
		return appAuditingAreasDAO.selectAuditingArea(begin, pageSize, orderByField, orderByMode, siteid, areaname);
	}

	@Override
	public int selectMapCount(String siteid, String areaname,String province,String city) {
		// TODO Auto-generated method stub
		return appAreasDAO.selectAreaCount(siteid, areaname,province,city);
	}
	
	@Override
	public int selectMapCountAuditing(String siteid, String areaname) {
		// TODO Auto-generated method stub
		return appAuditingAreasDAO.selectAuditingAreaCount(siteid, areaname);
	}

	@Override
	public List<BatDstarea> selectByAreaname(Map<String,String> map) {
		// TODO Auto-generated method stub
		return batDstareaDAO.selectByAreaname(map);
	}

	@Override
	public String selectOfSiteByAreaname(String areaname) {
		// TODO Auto-generated method stub
		return tbareainfoDAO.selectByAreaname(areaname);
	}
	
	@Override
	public int delAuditingMap(String areaid) {
		// TODO Auto-generated method stub
		return appAuditingAreasDAO.delAuditingByAreaid(areaid);
	}
	
	@Override
	public int checkedMapByAreaid(String areaid) {
		// TODO Auto-generated method stub
		return appAuditingAreasDAO.checkedMapByAreaid(areaid);
	}
	@Override
	public TblOaAuthRole selectRoleIdByRoleDesc(String roleDesc){
		return oaAuthRoleDAO.selectRoleIdByRoleDesc(roleDesc);
	}

	@Override
	public List<TbAppAreas> selectMapByCity(String city,String areaid) {
		// TODO Auto-generated method stub
		Map<String,String> map=new HashMap();
		map.put("city", city);
		map.put("areaid", areaid);
		return appAreasDAO.selectAreaByCity(map);
	}

	@Override
	public List<TbAppAreas> selectLngLat(String city) {
		// TODO Auto-generated method stub
		return appAreasDAO.selectLngLat(city);
	}

	
}
